"""
获取豆瓣电影Top250电影名称
BeautifulSoup
    ~ select ---> 根据选择器获取元素的列表
    ~ select_one ---> 根据选择器获取第一个匹配的元素

bs4.element.Tag ---> Tag对象就对应页面上的一个标签
    ~ text ---> 获取标签里面的文本内容
    ~ attrs ---> 获取标签的属性（字典）

官方文档：https://beautifulsoup.readthedocs.io/zh_CN/v4.4.0/

CSV文件 - Comma Seperate Value
    ~ import csv
    ~ writer ---> writerow([...])
    ~ reader --->
"""
import csv
import random
import time

import bs4
import requests

for page in range(10):
    resp = requests.get(
        url=f'https://movie.douban.com/top250?start={page * 25}',
        headers={
            'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) '
                          'AppleWebKit/537.36 (KHTML, like Gecko) '
                          'Chrome/88.0.4324.192 Safari/537.36'
        }
    )
    if resp.status_code == 200:
        soup = bs4.BeautifulSoup(resp.text, 'html.parser')
        title_spans_list = soup.select('div.info > div.hd > a > span:nth-child(1)')
        rating_spans_list = soup.select('div.info > div.bd > div > span.rating_num')
        with open('movie_data.csv', 'w') as file:
            writer = csv.writer(file, delimiter='#')
            writer.writerow(['标题', '评分'])
            for title_span, rating_span in zip(title_spans_list, rating_spans_list):
                writer.writerow([title_span.text, rating_span.text])
    time.sleep(random.randint(2, 5))
